﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.ComponentModel.DataAnnotations;
using SCMS.Model;
using SCMS.CoreBusinessLogic.ExpenseClaim;

namespace SCMS.UI.Models
{
    public class ExpenseClaim
    {
        public Model.ExpenseRequest EntityExpseRqst { get; set; }

        [Required]
        public Guid ClaimBy
        {
            get { return EntityExpseRqst.ClaimBy; }
            set { EntityExpseRqst.ClaimBy = value; }
        }

        [Required]
        public Guid EcfCurrencyId {
            get { return EntityExpseRqst.ExpenseCurrencyId; }
            set { EntityExpseRqst.ExpenseCurrencyId = value; }
        }
        public SelectList Staffs { get; set; }

        public SelectList Currencies { get; set; }

        public List<ExpenseClaimItem> EcfItems { get; set; }
    }

    public class ExpenseClaimItem
    {
        public Model.ExpenseItem EntityExpseItem { get; set; }

        public SelectList ProjectNos { get; set; }

        [Required(ErrorMessage = "Project No. is required")]
        public Guid ProjectDonorId { get; set; }

        public SelectList BudgetLines { get; set; }

        [Required(ErrorMessage = "Amount is required")]
        public decimal Amount
        {
            get { return EntityExpseItem.ItemAmount; }
            set { EntityExpseItem.ItemAmount = value; }
        }
    }

    public class passParams
    {
        public Guid ExpsCId { get; set; }

        public Guid Converted2currencyId { get; set; }
    }

    public class ExpsClaimItems
    {
        public Model.ExpenseItem EntityExpseItem { get; set; }

        public Model.Currency Currency { get; set; }

        public Guid? AccountCode { get; set; }

        [Required(ErrorMessage = "Amount is required")]
        public decimal Amount
        {
            get { return EntityExpseItem.ItemAmount; }
            set { EntityExpseItem.ItemAmount = value; }
        }

        public SelectList AccountCodes { get; set; }

        public Guid ProjectDonorId { get; set; }

        [Required(ErrorMessage = "Budget Line is required")]
        public Guid BudgetLineId { get; set; }

        public SelectList ProjectDonors { get; set; }

        public SelectList BudgetLines { get; set; }
    }

    public class ViewExpsClaims
    {
        public Model.ExpenseRequest EntityExpseRqst { get; set; }

        public List<Model.ExpenseItem> ExpsItems { get; set; }

        public Model.VStaffDetail PreparedBy { get; set; }

        public Model.VStaffDetail ApprovedBy { get; set; }

        public Model.VStaffDetail AuthorizedBy { get; set; }

        public Model.VStaffDetail ClaimBy { get; set; }

        public Model.VStaffDetail PostedBy { get; set; }

        public Model.Currency ExpsCurrency { get; set; }

        public Model.Currency Converted2Currency { get; set; }

        [Required(ErrorMessage = "Conversion Rate is required")]
        [DataType(DataType.Currency)]
        public decimal ConversionRate { get; set; }

        [Required(ErrorMessage = "Conversion Currency is required")]
        public Guid ConversionCurrencyId { get; set; }

        public SelectList Currencies { get; set; }

        [Required(ErrorMessage = "Master Budget Currency is required")]
        public Guid MBCurrencyId { get; set; }

        [Required(ErrorMessage = "Master Budget Value is required")]
        [DataType(DataType.Currency)]
        public decimal MBValue { get; set; }

        public List<ExpsClaimItems> ExpenseClaimItems { get; set; }

        public bool CanEdit { get; set; }

        public bool CanApprove { get; set; }

        public bool CanPostFunds { get; set; }
    }

    public class RejectECF
    {
        [Required]
        public Guid Id { get; set; }

        public string RefNumber { get; set; }

        [Required(ErrorMessage = "Remarks are required.")]
        public string RejectedAuthRemarks { get; set; }

        [Required(ErrorMessage = "Remarks are required.")]
        public string RejectedReviewRemarks { get; set; }

        public bool IsReview { get; set; }
    }

    public class ExpsClaimExtension
    {
        public static ViewExpsClaims PrepareECFModel(IExpenseClaimService ECService, Guid expId)
        {
            using (var db = new SCMSEntities())
            {
                Model.ExpenseRequest ExpsitemEntity = db.ExpenseRequests.First(p => p.Id == expId);
                var model = new ViewExpsClaims()
                {
                    EntityExpseRqst = ExpsitemEntity,
                    ExpsItems = ECService.GetExpenseItems(ExpsitemEntity.Id),
                    PreparedBy = db.VStaffDetails.First(p => p.StaffID == ExpsitemEntity.PreparedBy),
                    ExpsCurrency = db.Currencies.First(p => p.Id == ExpsitemEntity.ExpenseCurrencyId),
                    Converted2Currency = db.Currencies.First(p => p.Id == ExpsitemEntity.CurrencyConvertedTo),
                    ClaimBy = db.VStaffDetails.FirstOrDefault(p => p.StaffID == ExpsitemEntity.ClaimBy),
                    PostedBy = db.VStaffDetails.FirstOrDefault(p => p.StaffID == ExpsitemEntity.PostedBy),
                    ApprovedBy = db.VStaffDetails.FirstOrDefault(p => p.StaffID == (ExpsitemEntity.ReviewedBy == null ? Guid.Empty : ExpsitemEntity.ReviewedBy)),
                    AuthorizedBy = db.VStaffDetails.FirstOrDefault(p => p.StaffID == (ExpsitemEntity.AuthorizedBy == null ? Guid.Empty : ExpsitemEntity.AuthorizedBy))
                };
                //dummies
                var person = ExpsitemEntity.Staff != null ? ExpsitemEntity.Staff.Person : null;
                person = ExpsitemEntity.Staff1 != null ? ExpsitemEntity.Staff1.Person : null;
                person = ExpsitemEntity.Staff2 != null ? ExpsitemEntity.Staff2.Person : null;
                person = ExpsitemEntity.Staff3 != null ? ExpsitemEntity.Staff3.Person : null;
                person = ExpsitemEntity.Staff4 != null ? ExpsitemEntity.Staff4.Person : null;

                return model;
            }
        }
    }
}